﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BingVis.Common;
using System.IO;

namespace BingVis.GoogleTransit
{
    public class TransitStopLoader : TransitLoaderBase<ITransitStop>, ITransitStopLoader
    {
            

        public TransitStopLoader(IGTFSFile file)
            : base(file)
        {
            // If the filename isn't stops, throw.
            if (!Path.GetFileNameWithoutExtension(file.FileName).Equals("stops", StringComparison.CurrentCultureIgnoreCase))
                throw new ArgumentException("This loader will only accept files named stops.txt");
            
        }

        public override void ProcessItem(ICSVLine line)
        {
            bool processedSuccess = true;
            // "stop_id,stop_code,stop_name,stop_desc,stop_lat,stop_lon,zone_id,location_type,parent_station",
            TransitStop stop = new TransitStop();
            try
            {
                stop = new TransitStop()
                {
                    Id = line["stop_id"],
                    Code = line["stop_code"],
                    Name = line["stop_name"],
                    Description = line["stop_desc"],
                    Latitude = double.Parse(line["stop_lat"]),
                    Longitude = double.Parse(line["stop_lon"]),
                    Zone_Id = line["zone_id"],
                    Location_Type = line["location_type"],
                    Parent_Station = line["parent_station"],
                };
                processedSuccess = true;
            }
            catch (Exception)
            {
                processedSuccess = false;
            }

            if (processedSuccess)
            {
                Items.Add(stop);
                OnItemLoaded(stop);
            }
        }
        
    }
}
